package com.quicklife.service.impl;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import com.quicklife.pojo.Coupons;
import com.quicklife.service.inter.WebPushMessageService;
import com.quicklife.util.JsonUtil;
import com.quicklife.util.SqlHelper;
import com.quicklife.util.StringUtil;

/**
 * 消息推送管理
 * 
 * 
 * @author HackerD
 * 
 */
public class WebPushMessageServiceImpl implements WebPushMessageService {

	@Override
	public String addMessage(HttpServletRequest request) {
		try {
			String busid = request.getParameter("busid");
			String cpid = request.getParameter("cpid");
			String msg = request.getParameter("msg");
			String overdue = request.getParameter("overdue");
			String type = request.getParameter("type");
			String area = request.getParameter("area");
			String tableId = StringUtil.toString(SqlHelper
					.getTableId("message"));
			// 查询该商家是否有两条优惠在推送中
			String beTwo = "select id from message where fromid=? and status=0";
			List list = SqlHelper.executeQuery(beTwo, new String[] { busid },
					null);
			if (list.size() >= 2) {
				return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_FAIL,
						"推送失败,最多只能推送两条优惠");
			}

			// 查询该优惠是否已在推送列表中且正在推送中
			String result = SqlHelper.checkBeExist(
					"message where status=0 and couponid=?",
					new String[] { cpid }, "该优惠已在推送中");
			if (!StringUtil.isNull(result)) {
				return result;
			}
			String sql = "insert into message(id,type,fromid,overdue,area,msg,couponid) values(?,?,?,?,?,?,?)";
			Object[] params = new Object[] { tableId, type, busid,
					StringUtil.stringToSqlDate(overdue.split(" ")[0]), area,
					msg, cpid };

			SqlHelper.executeUpdate(sql, params);

			return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_OK, null);
		} catch (Exception e) {
			e.printStackTrace();
			return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_FAIL,
					StringUtil.RESULT_EXCEPTION);
		}
	}

	@Override
	public String deleteMessage(HttpServletRequest request) {
		try {
			String tableID = request.getParameter("id");

			String sql = "update message set status=2 where id=?";
			Object[] params = new Object[] { tableID };

			SqlHelper.executeUpdate(sql, params);

			return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_OK, null);
		} catch (Exception e) {
			e.printStackTrace();
			return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_FAIL,
					StringUtil.RESULT_EXCEPTION);
		}
	}

	@Override
	public String getMessage(HttpServletRequest request) {
		try {
			String busid = request.getParameter("busid");

			String beTwo = "select a.id,a.type,b.name busname,to_char(a.push_time,'yyyy-MM-dd HH:mm:ss') push_time,"
					+ "to_char(a.overdue,'yyyy-MM-dd') overdue,a.area,a.msg,c.name cpname,status,"
					+ "case status  when 0 then '推送中' when 2 then '终止' when 3 then '过期' end as statusname  from message a "
					+ "inner join business b on a.fromid=b.id inner join coupons c on a.couponid=c.id "
					+ "where a.fromid=?";
			List list = SqlHelper.executeQuery(beTwo, new String[] { busid },
					null);
			String rtInfos = JsonUtil.createRtJsonStr(
					StringUtil.RESULT_CODE_OK, list);
			JSONObject jsonObject = JSONObject.fromObject(rtInfos);

			list = SqlHelper.executeQuery(
					"select t.id from message t where t.fromid=?",
					new String[] { busid }, Coupons.class);
			if (list!=null&&list.size() > 0) {
				jsonObject.put("total", list.size());
			} else {
				jsonObject.put("total", 0);
			}

			return jsonObject.toString();
		} catch (Exception e) {
			e.printStackTrace();
			return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_FAIL,
					StringUtil.RESULT_EXCEPTION);
		}
	}

}
